package win.zxshuo.www.spider;

import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import win.zxshuo.www.service.TargetUrlService;

import java.util.ArrayList;
import java.util.List;

@Component
public class BaiduSearch extends BaseSpider {

    @Autowired
    TargetUrlService targetUrlService;

    // 百度搜索的基础URL
    private final  static String baseBaiduUrl = "https://www.baidu.com/s?ie=UTF-8&wd=";

    private Log logger = LogFactory.getLog(getClass());

    public Elements getSearchContent(String keyword){
        Elements elements = new Elements();
        String encodedUrl = baseBaiduUrl + encode(keyword);
        try {
            Document document = getPageContent(encodedUrl);
            Elements resultList = getSearchResult(document);
            List<String> urls= getUrlListFromBaiduResultElements(resultList);
            for (String url:urls){
                System.out.println(url);
            }
        }catch (Exception e){
            logger.info("获取百度网页的时候出错了");
            logger.error(e);
        }
        return elements;
    }

    public List<String> getSearchResultList(String keyWrod){
        List<String> resutls = new ArrayList<>();
        String encodedUrl = baseBaiduUrl + encode(keyWrod);
        try{
            Document document = getPageContent(encodedUrl);
            Elements theResultList = getSearchResult(document);
            resutls = getUrlListFromBaiduResultElements(theResultList);
        }catch (Exception e){
            logger.info("");
            logger.error(e.getCause());
        }
        return resutls;
    }

    public List<String> getUrlListFromBaiduResultElements(Elements elements){
        List<String> urls = new ArrayList<>();
        for (Element element:elements){
           String url =  element.selectFirst("h3").selectFirst("a").attr("href");
           BaseRequest baseRequest = new BaseRequest();
           String dest = baseRequest.getDestUrl(url);
           urls.add(dest);
        }
        return urls;
    }



    private Elements getSearchResult(Document document){
        return document.select(".result.c-container");
    }




}
